package com.teradata.filter;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

//@WebFilter
public class HelloFilterForListener implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(">>HelloFilterForListener init<<");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println(">>HelloFilterForListener doFilter<<");
        String valueOfKey = request.getParameter("key");
        Object attribute = request.getServletContext().getAttribute("key");
        if (null != valueOfKey && attribute.equals(valueOfKey)){
            System.out.println(">>HelloFilterForListener match<<");
            chain.doFilter(request, response);
        } else {
            System.out.println(">>HelloFilterForListener param error<<");
            PrintWriter out = response.getWriter();
            out.print("HelloFilterForListener param error");
            out.close();
        }
    }

    @Override
    public void destroy() {
        System.out.println(">>HelloFilterForListener destroyed<<");
    }
}
